﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CS490_Capstone___Stubby.Web_Pages
{
    public partial class GetTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String browserHeight;
            int contentHeight;
            
            // read screen height passed through URL
            if (Request.QueryString.ToString() != "")
            {
                HtmlGenericControl searchBody = (HtmlGenericControl)Page.Master.FindControl("myBody");
                // displays the appropriate header menu
                searchBody.Attributes.Add("onload", "showJavaScriptHeader();");

                // sets browser height to the visible browser height as passed by the query string
                // sets browser height to minimum of 550px if browser height is smaller
                browserHeight = (int.Parse(Request.QueryString["height"].ToString()) > 550) 
                    ? Request.QueryString["height"].ToString() : "550";

                getTicket(Request.QueryString["eName"].ToString(), Request.QueryString["vName"].ToString());
            }
            else
            {
                // sets browser height to default 800px if browser height cannot be determined
                browserHeight = "800";
            }

            // calculate and set the size of the input panel to fill the screen
            content.Height = contentHeight = int.Parse(browserHeight) - 120;
            topSpacer.Height = ((contentHeight - 70) / 2) - 15 > 0 ? ((contentHeight - 70) / 2) - 15 : 0; 
        }

        protected void getTicket(string eventName, string venueName)
        {
            // create webservice object
            WebServiceApplication.WebService webservice = new WebServiceApplication.WebService();

            // get event using searchTerm
            _OpenEvent ev = webservice.getSearchedEvent(eventName);

            // get a list of venues for that event
            List<_Venue> myList = webservice.getVenuesFromEvent(ev.eventName);

            lblEventName.Text = ev.eventName;

            foreach (_Venue venue in myList)
            // steps through each venue in the list
            {
                if (venue.venueName == venueName)
                {
                    // display event info
                    lblEventDate.Text = venue.dateStart.ToShortDateString() + " in ";
                    lblVenueCity.Text = venue.city + ", ";
                    lblVenueState.Text = venue.state + " at the ";
                    lblVenueName.Text = venue.venueName;

                    // get a list of sections
                    List<_Section> mySections = webservice.getSectionsInVenue(ev.eventName, venue.venueName);

                    // build the section list box
                    foreach (_Section section in mySections)
                    {
                        if (section.sectionAvailable)
                        {
                            sectionList.Items.Add(new ListItem(section.sectionName));
                        }                        
                    }                    

                    break;
                }
            }

            // Somehow I need to populate each of the three list boxes with sections, rows, and seats.
            // The code for adding list items to the list box is good, and the method for filling that box
            //   may be good as well. I would like to make it so that when the selected index of 
            //   sectionList is changed that rowList is enabled and populates. Then, once rowList's
            //   selected index changes seatList is enabled and populates. Once seatList's selected index
            //   is changed, a method fires off that determines the price of the ticket and executes:

            submitTable.Visible = true;
            ticketCost.Text += "/*cost of the ticket*/";
            // The code-behind for selectedTicket would pass the event, venue, and ticket info to the
            //    Checkout page, where the tables can be created that show the tickets selected and grand 
            //    totals.
        }
    }
}